/*******************************************************************************

PooledRegions analysis_FigA3.do

2022.09.02 (DJ) Created

*******************************************************************************/


	use "${analysis}/Kilimanjaro ViltoAV_censusappended.dta", clear
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_name ward district using "${analysis}/Kilimanjaro google_ViltoCities.dta"
		drop _merge
		rename (district ward village_name) (survey_district survey_ward survey_village)
		
		gen survey_region="Kilimanjaro"
		
		tempfile kili_vilaccess
		save `kili_vilaccess'
		
	// Manyara
		use "${analysis}/Manyara ViltoAV_censusappended.dta", clear
		merge 1:1 village_id using "${analysis}/Manyara ViltoMaizemarket.dta"
		drop _merge
		merge 1:1 village_id using "${analysis}/Manyara ViltoCities_dist.dta"
		drop _merge
		
	append using `kili_vilaccess'
	
	fsum `market_access_varlist'
	
	// generate MARKET ACCESS PROXIES
		foreach var in city_km city_hrs nearcity_km{
			gen l_google_vil_`var' = log(google_vil_`var')
			egen std_google_vil_`var' = std(google_vil_`var')
		}	
		

		gen DH_access = .
		foreach city in moshi arusha babati dodoma tanga{
			replace DH_access = 0 if !mi(google_vil_`city'_km)
		}
		foreach city in moshi arusha babati dodoma tanga{
			gen tau_`city' = 2.7*(0.9392 + 0.02019*google_vil_`city'_km)/25
			replace DH_access = DH_access + (1 + tau_`city')^(${elasticity})*pop_frac_`city'
		}
		egen DH_access_std = std(DH_access)
		replace DH_access_std = -DH_access_std
	
	// regression between two remoteness measures
		reg std_google_vil_city_km DH_access_std, robust
		sum google_vil_city_km if e(sample)
		sum DH_access if e(sample)
	
	
	// distribution of remoteness proxies
		twoway (kdensity DH_access_std) (kdensity std_google_vil_city_km, lp(-)), ///
		xlabel(-3(1)3) graphregion(color(white)) legend(order(1 "DH Remoteness" 2 "Distance Remoteness")) ///
		xtitle("Standardized Value of Remoteness Proxies") ytitle("Density")
		graph export "${pool_results}/density_remoteness_proxies.pdf", replace
		
		
